package com.ztgf.commodity.api.client;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.commodity.models.bo.CommodityCategoryBO;
import com.ztgf.commodity.models.dto.BrandInfoDTO;
import com.ztgf.commodity.models.dto.CommodityCategoryDTO;
import com.ztgf.commodity.models.dto.CommodityLabelDTO;
import com.ztgf.commodity.models.service.ICategoryBrandAttributesRelationService;
import com.ztgf.commodity.models.service.ICommodityCategoryService;
import com.ztgf.commodity.models.vo.*;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.CustomizeUtil;
import com.ztgf.search.models.bean.BrandsBean;
import com.ztgf.search.models.bean.SearchRequestBean;
import com.ztgf.search.models.bean.SearchResponseBean;
import com.ztgf.search.models.service.SearchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static com.ztgf.common.utils.UtilConstants.isNull;

/**
 * author: chenyu.wang
 * date: 2020-05-25 12:01
 * Description:
 */
@RestController
@RequestMapping("/api/commodityCategory")
@Slf4j
@Api(value = "CommodityCategoryApiController", tags = "商品分类表api接口")
public class CommodityCategoryApiController {

    @DubboReference
    private ICommodityCategoryService categoryService;
    @DubboReference
    private ICategoryBrandAttributesRelationService categoryBrandAttributesRelationService;
    @DubboReference
    private SearchService searchService;

    @PostMapping("/queryCategoryTree")
    @ApiOperation(value = "商品类目树形结构", notes = "商品类目树形结构")
    public Result queryCategoryTree(Long parentId, Long id,Long shopId) {
        List<CommodityCategoryTreeVO> tree = categoryService.queryNewCategoryTree(shopId);
        if(parentId != null){
            List<CommodityCategoryTreeVO> list = new ArrayList<>();
            if(tree != null && tree.size() > 0){
                for(CommodityCategoryTreeVO treeVO : tree){
                    if(treeVO.getParentId().equals(parentId)){
                        list.add(treeVO);
                    }

                }
            }
            return new Result<>(list);
        }

        if(id != null){
            CommodityCategoryDTO dto = categoryService.selectById(id);
            List<CommodityCategoryTreeVO> list = new ArrayList<>();
            if(tree != null && tree.size() > 0){
                for(CommodityCategoryTreeVO treeVO : tree){
                    if(treeVO.getParentId().equals(dto.getParentId())){
                        list.add(treeVO);
                    }

                }
            }
            return new Result<>(list);
        }
        return new Result<>(tree);
    }

    @PostMapping("/queryByParentId")
    @ApiOperation(value = "根据父级id查询类目", notes = "根据父级id查询类目")
    public Result<List<CommodityCategoryDTO>> queryByParentId(Long id) {
        if (null == id) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        Wrapper<CommodityCategoryDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("parent_id", id).eq("is_publish", 1)
                .eq("status", 0);
        List<CommodityCategoryDTO> list = categoryService.selectList(wrapper);
        return new Result<>(list);
    }

    @PostMapping("/getByChildId")
    @ApiOperation(value = "查询类目父级", notes = "查询类目父级")
    public Result getByChildId(Long id) {
        if (null == id) {
            return new Result<>(CodeEnum.FAIL_100002);
        } else {
            List<CommodityCategoryTreeVO> list = categoryService.getByChildId(id, new ArrayList<>());
            return new Result<>(list);
        }
    }

    @PostMapping("/queryShopAvailableArea")
    @ApiOperation(value = "查询店铺可售区域", notes = "查询店铺可售区域")
    public Result queryShopAvailableArea(Long shopId) {
        if (null == shopId) {
            return new Result<>(CodeEnum.FAIL_100002);
        } else {
            List<CommodityShopAvailableAreaVO> list = categoryService.queryShopAvailableArea(shopId);
            return new Result<>(list);
        }
    }

    @PostMapping("/queryShopCategory")
    @ApiOperation(value = "查询店铺所属类目", notes = "查询店铺所属类目")
    public Result queryShopCategory(Long shopId) {
        if (null == shopId) {
            return new Result<>(CodeEnum.FAIL_100002);
        } else {
            List<CommodityCategoryTreeVO> list = categoryService.queryShopCategory(shopId);
            return new Result<>(list);
        }
    }

    @PostMapping("/getFilterAttributes")
    @ApiOperation(value = "根据顶级类目id获取筛选属性，类目id可以为空", notes = "根据顶级类目id获取筛选属性，类目id可以为空")
    public Result getFilterAttributes(Long categoryId, String filterName) {
        List<CommodityFilterAttributesNewVO> list = categoryService.getFilterAttributes(categoryId, filterName);
        return new Result<>(list);
    }

    @PostMapping("/queryShopCategoryByBrandId")
    @ApiOperation(value = "查询店铺所属类目,根据品牌id", notes = "查询店铺所属类目,根据品牌id")
    public Result queryShopCategoryByBrandId(Long brandId, Long shopId) {
        if (null == brandId || null == shopId) {
            return new Result<>(CodeEnum.FAIL_100002);
        } else {
            List<CommodityCategoryTreeVO> list = categoryService.queryShopCategoryByBrandId(brandId, shopId);
            return new Result<>(list);
        }
    }

    @PostMapping(value = "/getSalesAttributes")
    @ApiOperation(value = "根据类目id和品牌id获取筛选属性,用于询价页", notes = "根据类目id和品牌id获取筛选属性,用于询价页")
    public Result getSalesAttributes(Long category, Long brandId) {
        if (null != category && null != brandId) {
            List<CategoryBrandAttributesVO> real = categoryService.getSalesAttributes(category, brandId);
            if (isNull(real)) {
                return new Result<>(Collections.EMPTY_LIST);
            } else {
                return new Result<>(real);
            }
        } else {
            return new Result<>(CodeEnum.FAIL_100002);
        }
    }

    @PostMapping(value = "/queryInquiryBrandByCategoryId")
    @ApiOperation(value = "商城询价页根据类目id获取品牌信息", notes = "商城询价页根据类目id获取品牌信息")
    public Result queryInquiryBrandByCategoryId(Long categoryId) {
        if (null != categoryId) {
            List<CommodityInquiryBrandVO> list = categoryService.queryInquiryBrandByCategoryId(categoryId);
            return new Result<>(list);
        } else {
            return new Result<>(CodeEnum.FAIL_100002);
        }
    }

    @PostMapping(value = "/getCommissionRate")
    @ApiOperation(value = "根据类目id获取佣金比例", notes = "根据类目id获取佣金比例")
    public Result getCommissionRate(Long id) {
        if (null == id) {
            return new Result<>(CodeEnum.FAIL_100002);
        } else {
            String rate = categoryService.getCommissionRate(id);
            return new Result<>(rate);
        }
    }

    @PostMapping("/queryApiPageList")
    @ApiOperation(value = "商品类目列表", notes = "商品类目列表")
    public Result queryPageList(CommodityCategoryBO categoryBO) {
        if (categoryBO.getParentId() == null) {
            categoryBO.setParentId(0L);
        }
        Wrapper<CommodityCategoryDTO> wrapper = new EntityWrapper<>();
        if (StringUtils.isNotBlank(categoryBO.getName())) {
            wrapper.like("name", categoryBO.getName());
        } else {
            wrapper.eq("parent_id", categoryBO.getParentId());
        }
        wrapper.orderBy("sort", true); //默认升序
        Page<CommodityCategoryDTO> page = new Page<>(categoryBO.getPageNo(), categoryBO.getPageSize());
        page = categoryService.selectPage(page, wrapper);
        Page<CommodityCategoryTreeVO> pageNew = new Page<>(categoryBO.getPageNo(), categoryBO.getPageSize());
        List<CommodityCategoryTreeVO> real = new ArrayList<>();
        List<CommodityCategoryDTO> list = page.getRecords();
        if (null != list && list.size() > 0) {
            for (CommodityCategoryDTO dto : list) {
                CommodityCategoryTreeVO treeVO = new CommodityCategoryTreeVO();
                treeVO.setId(dto.getId());
                treeVO.setName(dto.getName());
                treeVO.setInitials(dto.getInitials());
                treeVO.setParentId(dto.getParentId());
                treeVO.setSort(dto.getSort());
                treeVO.setCode(dto.getCode());
                treeVO.setIsDisplayNavBar(dto.getIsPublish());
                treeVO.setRate(dto.getDividendScale());
                real.add(treeVO);
            }
            pageNew.setRecords(real);
            pageNew.setTotal(page.getTotal());
            return new Result<>(pageNew);
        }
        return new Result<>(page);
    }

    @PostMapping(value = "/queryBrandByCategoryId")
    @ApiOperation(value = "根据类目id获取品牌信息", notes = "根据类目id获取品牌信息")
    public Result queryBrandByCategoryId(SearchRequestBean searchRequestBean) {
        SearchResponseBean responseBean = searchService.searchBrand("index_goods_2020", "goods", "goodsNameIkSmart^8,goodsNameIkMaxWord^5,brandNameIkSmart^7,brandNameIkMaxWord^4", searchRequestBean);
        if(CustomizeUtil.listIsNull(responseBean.getBrandList())){
            return new Result();
        }
        List<BrandsBean> list = responseBean.getBrandList();
        return new Result<>(list);
    }

    @PostMapping(value = "/queryLabelByCategoryIdAndBrandId")
    @ApiOperation(value = "根据类目id和品牌id获取标签属性信息", notes = "根据类目id和品牌id获取标签属性信息")
    public Result queryBrandByCategoryId(Long categoryId, Long brandId) {
        if (categoryId == null || brandId == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        List<CommodityLabelDTO> list = categoryBrandAttributesRelationService.queryLabelByCategoryIdAndBrandId(categoryId, brandId);
        return new Result<>(list);
    }


}
